package bavaria.hightech.testit.pruefungen.ws09;

import java.util.ArrayList;
import java.util.List;

public class GridLineSegment {
	

		private GridPoint startPoint;
		private GridPoint endPoint;
		
		public GridLineSegment(GridPoint startPoint, GridPoint endPoint) throws InvalidGridPointException{
			if(startPoint.row == endPoint.row || startPoint.col == endPoint.col){
			this.startPoint = startPoint;
			this.endPoint = endPoint;
			}else{
				throw new InvalidGridPointException("Start- und Endpunkt befinden sich nicht auf einer Linie");
			}
		}
		
		public int countGridPoints(){
			if(startPoint.row == endPoint.row){
				return endPoint.col - startPoint.col +1;
			}
			return endPoint.row - startPoint.row +1;
		}
		
		public List<GridPoint> allGridPointsAsList(){
			List<GridPoint> list = new ArrayList<GridPoint>();
			if(startPoint.col == endPoint.col){
				for (int i = startPoint.col; i <= endPoint.col; i++) {
					list.add(new GridPoint(startPoint.row, i));
				}
			}else{
				for (int j = startPoint.row; j < endPoint.row; j++) {
					list.add(new GridPoint(startPoint.col, j));
				}
			}
			return list;
		}
		
		public class InvalidGridPointException extends Exception{
			
			public InvalidGridPointException(String msg) {
				super(msg);
			}
		}
		
		
	}


